﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Datalayer;
using Entities;
using Common;


namespace QLHoNgheo
{
    public partial class frm_DieuDuong : Form
    {
        public frm_DieuDuong()
        {
            InitializeComponent();
        }
        bool _IsNew = false;
        IQueryMgr iq;
        DataTable dt;
        DataRow r;
        DoiTuongCoCong ur;

        private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            r = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            if (r != null)
            {
                ur = new DoiTuongCoCong();
                int ThanhVienID = int.Parse(r["Ma_DoiTuongCoCong"].ToString());
                iq = QueryMgr.GetDbInstance();
                ur = iq.DTCC_GetDoiTuongCoCongByID(ThanhVienID);
                ur.Ma_DoiTuongCoCong = ThanhVienID;
                txt_ghichu.Text = ur.GhiChu;
                txt_ten.Text = ur.Ten_DoiTuongCocong;
                lk_mdd.EditValue = ur.Ma_MucDieuDuong;
            }
        }

        void load_dgv()
        {
            cl_Lookupedit.lucb_MucDoDieuDuong(ref lk_mdd);
            cl_grvLookupedit.rpcb_MucDoDieuDuong(ref rplk_mucdieuduong);
            iq = QueryMgr.GetDbInstance();
            dt = new DataTable();
            dt = Cl_Convert.ToDataTable(iq.DTCC_GetAllDoiTuongCoCong());
            gridControl1.DataSource = dt;
        }

        private void frm_DieuDuong_Load(object sender, EventArgs e)
        {
            load_dgv();
        }

        private void bt_new_Click(object sender, EventArgs e)
        {
            txt_ten.Text = "";
            txt_ghichu.Text = "";
            lk_mdd.EditValue = "";
            _IsNew = true;
            bt_del.Text = "Hủy bỏ";
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            try
            {
                if (_IsNew == true)
                {
                    ur = new DoiTuongCoCong();
                    ur.Ma_MucDieuDuong =int.Parse( lk_mdd.EditValue.ToString());
                    ur.IsDeleted = false;
                    ur.GhiChu = txt_ghichu.Text;
                    ur.Ten_DoiTuongCocong = txt_ten.Text;

                    iq = QueryMgr.GetDbInstance();
                    iq.Dtcc_NewDoiTuongCoCong(ur);
                    MessageBox.Show("Thêm mới thông tin thành công");

                    bt_new.Enabled = true;
                    _IsNew = false;
                    bt_del.Text = "Xóa";
                }
                else
                {
                    ur.Ma_MucDieuDuong = int.Parse(lk_mdd.EditValue.ToString());
                    ur.IsDeleted = false;
                    ur.GhiChu = txt_ghichu.Text;
                    ur.Ten_DoiTuongCocong = txt_ten.Text;

                    iq = QueryMgr.GetDbInstance();
                    iq.Dtcc_EditDoiTuongCoCong(ur);
                    MessageBox.Show("Thay đổi thông thành công");
                }
                load_dgv();
            }
            catch { }
        }

        private void bt_del_Click(object sender, EventArgs e)
        {
            if (_IsNew == true)
            {
                bt_new.Enabled = true;
                _IsNew = false;
            }
            else
            {
                if (MessageBox.Show("Bạn muốn xóa thành viên này?", "Thực hiên chức năng xóa",
                   MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    iq = QueryMgr.GetDbInstance();
                    iq.Dtcc_DelOrResDoiTuongCoCong(ur.Ma_DoiTuongCoCong);
                    MessageBox.Show("Xóa thành công");
                }
            }
            load_dgv();
        }
    }
}
